package cn.com.gas.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiOperation;
import cn.com.common.constants.Constants;
import cn.com.common.result.DataResult;
import cn.com.gas.entity.GasForewarningDataEntity;
import cn.com.gas.entity.GasStationinfoEntity;
import cn.com.gas.service.GasForewarningDataService;
import cn.com.gas.service.GasStationinfoService;
import cn.com.system.entity.SysDept;
import cn.com.system.service.DeptService;
import cn.com.system.service.impl.HttpSessionService;
import cn.com.util.TsBqUtils;



/**
 * 数据异常预警
 *
 * @author shaochangyang
 * @email l2l84o7o5@live.com
 * @date 2024-07-28 20:40:18
 */
@Controller
@RequestMapping("/gas/forewarning")
public class GasForewarningDataController {
    @Autowired
    private GasForewarningDataService gasForewarningDataService;
    @Autowired
	private HttpSessionService httpSessionService;
    @Autowired
	private GasStationinfoService gasStationinfoService;
    @Autowired
   	private DeptService deptService;
    
    @SuppressWarnings({ "unchecked", "rawtypes", "unused" })
	@ApiOperation(value = "查询分页数据")
    @PostMapping("/list")
    // @RequiresPermissions("gasForewarningData:list")
    @ResponseBody
    public DataResult findListByPage(@RequestBody GasForewarningDataEntity gasForewarningData) throws ParseException{
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    	String currentDeptId = httpSessionService.getCurrentDeptId();
    	
    	String taxorgcode = gasForewarningData.getTaxorgcode();
    	
    	Page page = new Page(gasForewarningData.getPage(), gasForewarningData.getLimit());
        
		LambdaQueryWrapper<GasStationinfoEntity> stationinfoQueryWrapper = Wrappers.lambdaQuery();

        //查询条件示例
        if (TsBqUtils.isNotNull(taxorgcode)) {
        	stationinfoQueryWrapper.eq(GasStationinfoEntity::getType, "group");
        	stationinfoQueryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
        	stationinfoQueryWrapper.eq(GasStationinfoEntity::getTaxorgcode, taxorgcode);

		} else {
			if (Constants.TAX_COUNTES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 县区局
				stationinfoQueryWrapper.eq(GasStationinfoEntity::getType, "group");
				stationinfoQueryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
				stationinfoQueryWrapper.eq(GasStationinfoEntity::getTaxorgcode, currentDeptId);

			} else if (Constants.TAX_CITIES.equals(TsBqUtils.getTaxOrgLevel(currentDeptId))) {
				// 市局
				stationinfoQueryWrapper.eq(GasStationinfoEntity::getType, "group");
				stationinfoQueryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");
				stationinfoQueryWrapper.like(GasStationinfoEntity::getTaxorgcode, currentDeptId.substring(0, 5));
			} else {
				// 省局
				stationinfoQueryWrapper.eq(GasStationinfoEntity::getType, "group");
				stationinfoQueryWrapper.eq(GasStationinfoEntity::getIsAnalyse, "Y");

			}
		}        
        
        if (TsBqUtils.isNotNull(gasForewarningData.getStationinfoName())) {
			stationinfoQueryWrapper.like(GasStationinfoEntity::getName, gasForewarningData.getStationinfoName());
		}
        
        List<GasStationinfoEntity> stationinfoList = gasStationinfoService.list(stationinfoQueryWrapper);
        List<String> listStationId = new ArrayList<>();
		for (GasStationinfoEntity stationinfo : stationinfoList) {
			String id = stationinfo.getId();
			listStationId.add(id);			
		}
		if(TsBqUtils.isNull(listStationId)){
			return DataResult.fail("无查询到加油站"); 
		}
    	LambdaQueryWrapper<GasForewarningDataEntity> queryWrapper = Wrappers.lambdaQuery();

    	queryWrapper.in(GasForewarningDataEntity::getStationinfoId, listStationId);
    	
    	
    	if (!StringUtils.isEmpty(gasForewarningData.getCreateTimeStart())) {
			/*queryWrapper.ge(GasForewarningDataEntity::getCreateTime,
					sdf.parse(gasForewarningData.getCreateTimeStart() + " 00:00:00"));*/
			
			queryWrapper.ge(GasForewarningDataEntity::getTime, gasForewarningData.getCreateTimeStart().trim());

		}
    	
		if (!StringUtils.isEmpty(gasForewarningData.getCreateTimeEnd())) {
			/*queryWrapper.le(GasForewarningDataEntity::getCreateTime,
					sdf.parse(gasForewarningData.getCreateTimeEnd() + " 23:59:59"));*/
			
			queryWrapper.le(GasForewarningDataEntity::getTime, gasForewarningData.getCreateTimeEnd().trim());
			
		}
    	
        IPage<GasForewarningDataEntity> iPage = gasForewarningDataService.page(page, queryWrapper);
        
        List<GasForewarningDataEntity> records = iPage.getRecords();
        
        for (GasForewarningDataEntity entity : records) {
        	String stationinfoId = entity.getStationinfoId();
			
			GasStationinfoEntity stationinfo = gasStationinfoService.getById(stationinfoId);
			String name = stationinfo.getName();
			entity.setStationinfoName(name);
			
			String _taxorgcode = stationinfo.getTaxorgcode();
			SysDept dept = deptService.getById(_taxorgcode);
        	
        	if(TsBqUtils.isNotNull(dept)){
        		entity.setTaxorgname(dept.getName());
        	}
        	
        	
		}
        
        return DataResult.success(iPage);
    }

}
